/**
 * 主题配置文件
 * 定义系统支持的各种主题样式
 */

// 主题类型定义
export interface Theme {
  name: string;
  id: string;
  colors: {
    primary: string;
    success: string;
    warning: string;
    danger: string;
    info: string;
    background: string;
    sidebar: string;
    header: string;
    text: string;
    border: string;
  };
  variables: {
    [key: string]: string;
  };
}

// 预设主题配置
export const themes: Theme[] = [
  {
    name: '默认主题',
    id: 'default',
    colors: {
      primary: '#409EFF',
      success: '#67C23A',
      warning: '#E6A23C',
      danger: '#F56C6C',
      info: '#909399',
      background: '#f0f2f5',
      sidebar: '#304156',
      header: '#ffffff',
      text: '#303133',
      border: '#DCDFE6'
    },
    variables: {
      '--el-color-primary': '#409EFF',
      '--el-color-success': '#67C23A',
      '--el-color-warning': '#E6A23C',
      '--el-color-danger': '#F56C6C',
      '--el-color-info': '#909399',
      '--el-bg-color': '#f0f2f5',
      '--el-bg-color-page': '#f0f2f5',
      '--el-text-color-primary': '#303133',
      '--el-text-color-regular': '#606266',
      '--el-border-color': '#DCDFE6',
      '--el-border-color-light': '#E4E7ED',
      '--el-border-color-lighter': '#EBEEF5',
      '--el-border-color-extra-light': '#F2F6FC',
      '--el-bg-color-sidebar': '#304156',
      '--el-bg-color-logo': '#2b2f3a'
    }
  },
  {
    name: '红色主题',
    id: 'red',
    colors: {
      primary: '#f5222d',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#fff1f0',
      sidebar: '#820014',
      header: '#ffffff',
      text: '#262626',
      border: '#ffa39e'
    },
    variables: {
      '--el-color-primary': '#f5222d',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#fff1f0',
      '--el-bg-color-page': '#fff1f0',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#ffa39e',
      '--el-border-color-light': '#ffa39e',
      '--el-border-color-lighter': '#fff1f0',
      '--el-border-color-extra-light': '#fff1f0',
      '--el-bg-color-sidebar': '#820014',
      '--el-bg-color-logo': '#5c0010'
    }
  },
  {
    name: '橙色主题',
    id: 'orange',
    colors: {
      primary: '#fa8c16',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#fff7e6',
      sidebar: '#873800',
      header: '#ffffff',
      text: '#262626',
      border: '#ffd591'
    },
    variables: {
      '--el-color-primary': '#fa8c16',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#fff7e6',
      '--el-bg-color-page': '#fff7e6',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#ffd591',
      '--el-border-color-light': '#ffd591',
      '--el-border-color-lighter': '#fff7e6',
      '--el-border-color-extra-light': '#fff7e6',
      '--el-bg-color-sidebar': '#873800',
      '--el-bg-color-logo': '#5c2900'
    }
  },
  {
    name: '黄色主题',
    id: 'yellow',
    colors: {
      primary: '#faad14',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#fffbe6',
      sidebar: '#664d03',
      header: '#ffffff',
      text: '#262626',
      border: '#ffe58f'
    },
    variables: {
      '--el-color-primary': '#faad14',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#fffbe6',
      '--el-bg-color-page': '#fffbe6',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#ffe58f',
      '--el-border-color-light': '#ffe58f',
      '--el-border-color-lighter': '#fffbe6',
      '--el-border-color-extra-light': '#fffbe6',
      '--el-bg-color-sidebar': '#664d03',
      '--el-bg-color-logo': '#4a3802'
    }
  },
  {
    name: '绿色主题',
    id: 'green',
    colors: {
      primary: '#52c41a',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#f6ffed',
      sidebar: '#003300',
      header: '#ffffff',
      text: '#262626',
      border: '#b7eb8f'
    },
    variables: {
      '--el-color-primary': '#52c41a',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#f6ffed',
      '--el-bg-color-page': '#f6ffed',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#b7eb8f',
      '--el-border-color-light': '#b7eb8f',
      '--el-border-color-lighter': '#f6ffed',
      '--el-border-color-extra-light': '#f6ffed',
      '--el-bg-color-sidebar': '#003300',
      '--el-bg-color-logo': '#002200'
    }
  },
  {
    name: '蓝色主题',
    id: 'blue',
    colors: {
      primary: '#1890ff',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#f0f5ff',
      sidebar: '#001529',
      header: '#ffffff',
      text: '#262626',
      border: '#d9d9d9'
    },
    variables: {
      '--el-color-primary': '#1890ff',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#f0f5ff',
      '--el-bg-color-page': '#f0f5ff',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#d9d9d9',
      '--el-border-color-light': '#d9d9d9',
      '--el-border-color-lighter': '#f0f0f0',
      '--el-border-color-extra-light': '#f0f0f0',
      '--el-bg-color-sidebar': '#001529',
      '--el-bg-color-logo': '#000c1a'
    }
  },
  {
    name: '靛色主题',
    id: 'indigo',
    colors: {
      primary: '#2f54eb',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#f0f5ff',
      sidebar: '#061178',
      header: '#ffffff',
      text: '#262626',
      border: '#adc6ff'
    },
    variables: {
      '--el-color-primary': '#2f54eb',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#f0f5ff',
      '--el-bg-color-page': '#f0f5ff',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#adc6ff',
      '--el-border-color-light': '#adc6ff',
      '--el-border-color-lighter': '#f0f5ff',
      '--el-border-color-extra-light': '#f0f5ff',
      '--el-bg-color-sidebar': '#061178',
      '--el-bg-color-logo': '#030852'
    }
  },
  {
    name: '紫色主题',
    id: 'purple',
    colors: {
      primary: '#722ed1',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#f9f0ff',
      sidebar: '#22075e',
      header: '#ffffff',
      text: '#262626',
      border: '#d3adf7'
    },
    variables: {
      '--el-color-primary': '#722ed1',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#f9f0ff',
      '--el-bg-color-page': '#f9f0ff',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#d3adf7',
      '--el-border-color-light': '#d3adf7',
      '--el-border-color-lighter': '#f9f0ff',
      '--el-border-color-extra-light': '#f9f0ff',
      '--el-bg-color-sidebar': '#22075e',
      '--el-bg-color-logo': '#1a0547'
    }
  },
  {
    name: '粉色主题',
    id: 'pink',
    colors: {
      primary: '#eb2f96',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#fff0f6',
      sidebar: '#780650',
      header: '#ffffff',
      text: '#262626',
      border: '#ffadd2'
    },
    variables: {
      '--el-color-primary': '#eb2f96',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#fff0f6',
      '--el-bg-color-page': '#fff0f6',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#ffadd2',
      '--el-border-color-light': '#ffadd2',
      '--el-border-color-lighter': '#fff0f6',
      '--el-border-color-extra-light': '#fff0f6',
      '--el-bg-color-sidebar': '#780650',
      '--el-bg-color-logo': '#520339'
    }
  },
  {
    name: '青色主题',
    id: 'cyan',
    colors: {
      primary: '#13c2c2',
      success: '#52c41a',
      warning: '#faad14',
      danger: '#f5222d',
      info: '#8c8c8c',
      background: '#e6fffb',
      sidebar: '#006d75',
      header: '#ffffff',
      text: '#262626',
      border: '#87e8de'
    },
    variables: {
      '--el-color-primary': '#13c2c2',
      '--el-color-success': '#52c41a',
      '--el-color-warning': '#faad14',
      '--el-color-danger': '#f5222d',
      '--el-color-info': '#8c8c8c',
      '--el-bg-color': '#e6fffb',
      '--el-bg-color-page': '#e6fffb',
      '--el-text-color-primary': '#262626',
      '--el-text-color-regular': '#595959',
      '--el-border-color': '#87e8de',
      '--el-border-color-light': '#87e8de',
      '--el-border-color-lighter': '#e6fffb',
      '--el-border-color-extra-light': '#e6fffb',
      '--el-bg-color-sidebar': '#006d75',
      '--el-bg-color-logo': '#00474f'
    }
  }
];

// 应用主题到文档根元素
export function applyTheme(themeId: string): void {
  const theme = themes.find(t => t.id === themeId) || themes[0];
  const root = document.documentElement;
  
  // 清除旧的主题变量
  const oldVariables = Object.keys(root.style).filter(key => key.startsWith('--'));
  oldVariables.forEach(variable => {
    root.style.removeProperty(variable);
  });
  
  // 应用新的主题变量
  Object.entries(theme.variables).forEach(([key, value]) => {
    root.style.setProperty(key, value);
  });
  
  // 保存主题设置到本地存储
  localStorage.setItem('theme', themeId);
}

// 获取当前主题
export function getCurrentTheme(): Theme {
  const themeId = localStorage.getItem('theme') || 'default';
  return themes.find(t => t.id === themeId) || themes[0];
}

// 初始化主题
export function initTheme(): void {
  const savedThemeId = localStorage.getItem('theme') || 'default';
  applyTheme(savedThemeId);
}